<!--车站补款操作页面-->
<template>
  <div class="shouPiaoYuanPKCYDXC el-card is-always-shadow el-card__body">
    <avue-crud
      ref="crud"
      :option="option"
      :table-loading="listLoading"
      :data="data"
      :page.sync="page"
      @search-change="handleFilter"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="getList">
      <template slot="menuRight">
        <el-button class="filter-item" type="primary" size="mini" @click="supPaymentTimelimit(1)">设置补款期限</el-button>
        <el-button type="primary"  size="mini" @click="setDate">设置入账时间</el-button>
        <el-button type="primary"  size="mini" @click="notice">通知</el-button>
        <el-button type="primary"  size="mini" @click="exportData">导出</el-button>
      </template>
      <template slot-scope="scope" slot="lineCodeSearch">
        <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
          <el-option
            v-for="(item,index) in lineCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="stationCodeSearch">
        <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" clearable filterable>
          <el-option
            v-for="(item,index) in stationCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="diffAmountSearch" style="width: 500px;">
        <div style="width: 500px;">
          <el-form-item>
          <el-input-number v-model="searchForm.maxDiffAmount" controls-position="right" :min="1" ></el-input-number>~
          <el-input-number v-model="searchForm.minDiffAmount" controls-position="right" :min="1" ></el-input-number>
          </el-form-item>
        </div>
      </template>

      <template slot="searchMenu" slot-scope="{row,size}">
        <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);getList()}" icon="el-icon-delete">清空</el-button>
      </template>
    </avue-crud>
    <setDate ref="setDate" v-if="isShowDia"></setDate>
    <el-dialog
      title="设置补款期限"
      :visible.sync="supPaymentTimelimitDialogVisible"
      width="1500px" >
      <avue-crud
        ref="crud"
        :option="optionLimit"
        :table-loading="listLoadingLimit"
        :data="listLimit"
        @on-load="getLimitList">
        <template slot="supPaymentLimit" slot-scope="{row}">
          <el-input
            v-model="row.supPaymentLimit"
            placeholder="请输入天数"></el-input>
        </template>
        <!-- 
	<template slot="incomeTime" slot-scope="{row}">
          <el-date-picker
            v-model="row.incomeTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </template>-->
      </avue-crud>
      <template slot="footer">
        <center>
          <el-button type="primary" size="small"
                     @click="submit">确认
          </el-button>
          <el-button size="small"
                     @click="supPaymentTimelimitDialogVisible=false">取消</el-button>
        </center>
      </template>
    </el-dialog>
</div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import moment from "moment";
    import {
      chezhan_bukuan_status,
      chezhan_IS_ADD_NOTICE,
      clearSelect,
      fenExYuangettoFixed, getchezhan_bukuan_status, getChezhan_IS_ADD_NOTICE,
      getgetStationCodes,
      getLineCodes, getLineName, getStationName,
      getXieChaStatusFormat, setSearchFiledsEmpty,
      xieChaStatus,getStationByCode,getLineAndStationByCode,
    } from "../../../../../api/subwayRat/commonCheck";
    import {exportDataToExcel} from "../../../../../api/subwayRat/exportSelf";
    import setDate from "./setDate";
    import {
      getSwAuStationShortList,
      noticeSwAuStationShort,getSwAuStationShortLimit,tableOptionLimit,updateSwAuStationShortLimit
    } from "../../../../../api/subwayRat/conductorDifferences/conductorTicketChaYiShenhe/cheZhanPKCY/cheZhanBKTZ";
   export default {
      components:{
        setDate
      },
      data() {
            return {
              isShowDia:false, //添加页面
              supPaymentTimelimitDialogVisible: false,
              listLoadingLimit: true,
              listLoading: true,
              lineCodes:[],
              optionLimit: tableOptionLimit,
              stationCodes:[],
              tvmCodes:[],
              selectKeys:[], //存放对像
              selectKeyVals:[], //存放值
              data: [],
              searchForm:{
                lineCode:'',
                stationCode:'',
                isNotice: '0'
              },
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条,
                isAsc: false// 是否倒序
              },
              option:{ },
              listLimit : []
            }
        },
        created() {
        this.getOption();
        },
        methods: {
          setSearchFiledsEmpty,
          refreshChange() {
            this.getList()
          },
          async submit(){
            let p = {
              list : JSON.stringify(this.listLimit)
            };
            await updateSwAuStationShortLimit(p);
            this.$message.success("操作成功！");
            this.getList();
            this.supPaymentTimelimitDialogVisible = false;
          },
          getLimitList() {
            this.listLoadingLimit = true;
            let p = {
              ids : JSON.stringify(this.selectKeyVals),
              userCode : this.$store.getters.userInfo.userCode
            };
            getSwAuStationShortLimit(p).then((response)=>{
              this.listLimit = response.data.data
              this.listLoadingLimit = false
            });
          },
          supPaymentTimelimit(type){
            if(this.selectKeyVals.length<=0){
              this.$message.warning("请至少选择一条数据操作！");
              return;
            }
            for(let i=0;i<this.selectKeys.length;i++){
              if(1 == type && 1 == this.selectKeys[i].noticeState){
                this.$message.warning("勾选数据中存在已通知数据！");
                return
              }
            }
//        if(2 == type){
//          this.optionLimit.column[this.optionLimit.column.length-1].cell = false;
//        }else{
//          this.optionLimit.column[this.optionLimit.column.length-1].cell = true;
//        }
            this.getLimitList();
            this.supPaymentTimelimitDialogVisible = true;
          },
          getList() {
            this.listLoading = true;
            Object.assign(this.searchForm,this.page);
            let param={compareType:4};
            Object.assign(param,this.searchForm);
            getSwAuStationShortList(param).then((response)=>{
              this.data = response.data.data;
              this.listLoading = false;
              this.page.total=response.data.total;
            });
          },
          selectionChange(list){
            this.selectKeys=list;
            this.selectKeyVals=[];
            for(let i=0;i<list.length;i++){
              this.selectKeyVals.push(list[i].id);
            }
          },
          handleFilter(params, done) {
            Object.assign(this.searchForm,params);
            this.getList();
            done()
          },
          async notice(){
            if(this.selectKeys.length<=0){
              this.$message.warning("请选择通知数据！");
              return;
            }

            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].isAddNotice==1){
                this.$message.warning("请选择未通知数据！");
                return;
              }
            }
            let list = [];
            for(let i=0;i<this.selectKeys.length;i++){
              list.push({id:this.selectKeys[i].id,supPaymentLimit:this.selectKeys[i].supPaymentLimit});
            }
            let messageLSList = [];
            for(let i=0;i<this.selectKeys.length;i++){
              let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
              messageLSList.push({
                "content":"车站：" + data.data[0].name,
                "lineCode":this.selectKeys[i].lineCode,
                "stationCode":this.selectKeys[i].stationCode
              })
            }
            let p = {
              messageLSList:JSON.stringify(messageLSList),
              title:this.$store.getters.tag.label,
              msgType:"通知",
              type:"cheZhanBKTZ_001",
              list : JSON.stringify(list)
            };
            noticeSwAuStationShort(p).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
              }else{
                this.$message.error(res.data.msg);
              }
            })
          },
          setDate(row){ //回馈预览
            if(this.selectKeys.length<=0){
              this.$message.warning("至少选择一条数据！");
              return;
            }
            let th=this;
            this.isShowDia=true;
            this.$nextTick(()=>{
              this.$refs.setDate.getList(th.selectKeys);
            })
          },
          changeLine(v){ //改变线路时
            getgetStationCodes(v).then(res=>{
              this.stationCodes=res;
            });
            this.searchForm.stationCode='';
          },
          isassistInvestStatus(){ // 是否协查
            for(let i=0;i<this.selectKeys.length;i++){
              let row=this.selectKeys[i];
              if(row.assistStatus==3){
                return false
              }
            }
            return true;
          },
          async exportData(){ // 导出
            this.searchForm.pageNo=1;
            this.searchForm.pageSize=1000000000;
            if(this.selectKeys.length>0){
                let newData = JSON.parse(JSON.stringify(this.selectKeys));
                exportDataToExcel(newData,this.option.column,'车站补款通知');
            }else {
              let res=await getSwAuStationShortList(this.searchForm);
              let data = res.data.data;
              exportDataToExcel(data,this.option.column,'车站补款通知');
            }
          },
          getOption(){
            let th=this;
            this.option={
              emptyBtn:false,
              editBtn:false,
              delBtn:false,
              addBtn:false,
              border: true,
              index:true,
              indexLabel:'序号',
              page:true,
              align:'center',
              menu:false,
              menuWidth:150,
              menuAlign:'center',
              selection: true,
              reserveSelection:false,
              column:[{label:"线路",width:100,
                "prop":"lineCode",type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
              return getLineName(value);
            }},
                {label:"站点",width:180,
                  "prop":"stationCode",type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
              return getStationName(row.lineCode,value);
            }},
                {label:"收入日期",width:120,
                  "prop":"incomeTime",type:'date',format:'yyyy-MM-dd',valueFormat:'yyyy-MM-dd',search:true},
                {label:"短款金额",width:120,
                  "prop":"shortAmount",formatter:function (row,v) {
                    return fenExYuangettoFixed(row,'shortAmount');
                  }},

                {label:"车站人员",width:160,
                  "prop":"conductorPersonName"},
                {label:"车站人员工号",width:120,
                  "prop":"conductorJobNo"},
                {label:"通知状态",
                  "prop":"noticeState",type:'select',dicData:[{label:'已通知',value:1},{label:'未通知',value:0}],
                  formatter:function(row, value , label, column){
                  let state={1:'已通知',0:'未通知'};
                    return state[value];
                  }},
                {
                  hide: false,
                  label: '通知时间',
                  prop: 'noticeTime',
                  format: 'yyyy-MM-dd',
                  valueFormat: 'yyyy-MM-dd',
                  width: 90,
                  type:'date',
                  search: false
                },{
                  label: '通知人',
                  prop: 'noticeName',
                  width:160
                },
                {label:"补款状态", "prop":"isNotice",type:'select',search:true,searchValue:'0',dicData:chezhan_bukuan_status(),formatter:function (row,v) {
                    return getchezhan_bukuan_status(v);
                  }},
                {
                  width:140,
                  hide: false,
                  label: '是否期限内补款',
                  formatter:function(row, value , label, column){
                    return value == 1?'是':(value == 0?'否':'');
                  },
                  prop: 'isLimitSuppay',
                  type: 'select',
                  dicData: [{
                    label: '否',
                    value: 0
                  }, {
                    label: '是',
                    value: 1
                  }],
                  search: false
                }, 
                {label:"补款日期",width:120,
                  "prop":"extraMoneyTime",type:'date',valueFormat:'yyyy-MM-dd',format:'yyyy-MM-dd',search:true},
                {label:"入账日期",width:120,
                  "prop":"accountTime",type:'date',valueFormat:'yyyy-MM-dd',format:'yyyy-MM-dd',search:true},
                {label:"设置期限时间",width:120,
                  "prop":"supPaymentTimelimit",type:'date',valueFormat:'yyyy-MM-dd',format:'yyyy-MM-dd',search:false},
                {label:"设置期限天数",width:120,
                  "prop":"supPaymentLimit",search:false}

              ]
            }
          },
          getChaYi(row,compareFirst,compareSec){ //结账金额 sc 差额
            if(!row[compareFirst] || !row[compareSec]){
              return '';
            }
            return Number(row[compareFirst])-Number(row[compareSec]);
          }
        },
      mounted() {
        getLineCodes().then(res=>{ //获取线路
          this.lineCodes=res;
        });
      }
    }
</script>
<style lang="scss">

</style>
